package cloudContact.DAO;

import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import cloudContact.MyDAOException;

public class UserDAO {
	private BeanFactory<User> factory;
	
	private static UserDAO userDAO;
	
	public static UserDAO getInstance() throws RollbackException
	{
		if (userDAO == null)
		{
			userDAO = new UserDAO();
		}
		return userDAO;
	}

	private UserDAO() {
       
        	String jdbcDriver = "com.mysql.jdbc.Driver";
            String jdbcURL    = "jdbc:mysql:///finalproject";
            String userName   = "anonymous";
            String password   = "";
            String tableName = "users";
            BeanTable.useJDBC(jdbcDriver,jdbcURL,userName,password);
        	BeanTable<User> table = BeanTable.getSQLInstance(User.class,tableName,jdbcDriver,jdbcURL);
            if (!table.exists()) {
                table.create("UserId");   // provide the primary key property(s)
            }
            factory = table.getFactory();
	}

	public void create(User newUser) throws MyDAOException {
        try {
        	Transaction.begin();
        	User dbUser = factory.create();
        	factory.copyInto(newUser, dbUser);
            Transaction.commit();
            newUser.setUserId(dbUser.getUserId());
        } catch (DuplicateKeyException e) {
            throw new MyDAOException("Contact Extension already exists: "+ newUser.getUserId());
        } catch (RollbackException e) {
            throw new MyDAOException(e);
        }
    }

    public User lookup(int id) throws MyDAOException {
        try {
            return factory.lookup(id);
        } catch (RollbackException e) {
            throw new MyDAOException(e);
        }
    }
    
    public User lookup(String eMail) throws MyDAOException {
        try {
        	User[] users =  factory.match(MatchArg.equals("Email", eMail));
        	
        	if (users.length == 0) {
				return null;
			} else if (users.length > 1) {
				throw new MyDAOException("More than one record found in of user name : " + eMail);
			}
        	
        	return users[0];
        	
        } catch (RollbackException e) {
            throw new MyDAOException(e);
        }
    }
    
    public User lookupByGmail(String gMail) throws MyDAOException {
        try {
        	User[] users =  factory.match(MatchArg.equals("Email", gMail));
        	
        	if (users.length == 0) {
				return null;
			} else if (users.length > 1) {
				throw new MyDAOException("More than one record found with gmail : " + gMail);
			}
        	
        	return users[0];
        	
        } catch (RollbackException e) {
            throw new MyDAOException(e);
        }
    }
    
    public void update(User theUser) {
    	try {
			Transaction.begin();
	        int userId = theUser.getUserId();
	        User dbUser = factory.lookup(userId);
	        if (dbUser == null) {
	            throw new MyDAOException("User id does not exist: "+userId);
	        }

	        factory.copyInto(theUser, dbUser);
	        
	        Transaction.commit();
		} catch (RollbackException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MyDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    }
    
    public void delete(int id) throws MyDAOException{
    	try {
			factory.delete(id);
		} catch (RollbackException e) {
			throw new MyDAOException(e);
		}
    }
}
